package com.wanmait.zaisousuo.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.mapper.ArticleMapper;
import com.wanmait.zaisousuo.pojo.ArticleComment;
import com.wanmait.zaisousuo.service.ArticleCommentService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wanmait.zaisousuo.mapper.ArticleCommentMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ArticleCommentServiceImpl implements ArticleCommentService {

    @Resource
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    private ArticleMapper articleMapper;


    @Override
    public void insert(ArticleComment articleComment) {

        articleCommentMapper.addOneComment(articleComment);//添加一条评论
        articleMapper.commentCountAddOne(articleComment.getArticle().getId());//文章评论数+1
        if (articleComment.getParentArticleComment().getId()!=0){
            articleCommentMapper.increaseOneChildren(articleComment.getParentArticleComment().getId());//父级的子类数量+1
        }

    }

    @Override
    public void update(ArticleComment object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public ArticleComment findById(Integer id) {
        return null;
    }

    @Override
    public List<ArticleComment> findAll() {
        return null;
    }

    @Override
    public List<ArticleComment> findAll(ArticleComment object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(ArticleComment object) {
        return 0;
    }

    @Override
    public PageInfo<ArticleComment> findList(int pageNum) {
        return null;
    }

    //根据文章id查出所有评论
    @Override
    public List<ArticleComment> findAllByArticleId(Integer id) {
        return articleCommentMapper.findAllByArticleId(id);
    }


    //根据文章id查询出所有的顶级评论
    @Cacheable(value = "ArticleCache",key = "'findAllTopComments'+#pageNum+#id")
    @Override
    public PageInfo<ArticleComment> findAllTopComments(Integer id,Integer pageNum) {
        //设置页码和每页显示条数
        int pageSize=5;
        PageHelper.startPage(pageNum,pageSize);
        List<ArticleComment> articleComments = articleCommentMapper.findAllTopComments(id,pageNum);
        PageInfo<ArticleComment> pageInfo = new PageInfo<>(articleComments,5);
        return pageInfo;
    }


    //根据父级id获取所有的直接子类
    @Cacheable(value = "ArticleCache",key = "'findAllChildrenCommentsById'+#id")
    @Override
    public List<ArticleComment> findAllChildrenCommentsById(Integer id) {
        return articleCommentMapper.findAllChildrenCommentsById(id);
    }



}
